package com.zhangwei.controller;

import com.zhangwei.pojo.GlobalConfig;
import com.zhangwei.pojo.bean.ResultVO;
import com.zhangwei.service.GlobalConfigService;
import com.zhangwei.utils.ResultVoUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/manage/global")
@Slf4j
public class GlobalConfigMController {

    @Autowired
    GlobalConfigService globalConfigService;


    @RequestMapping("/modify")  // 修改ui
    public ModelAndView modify(@RequestParam(value = "id",required = false) int id,Map<String,Object> map){
            //map是传给模板的参数，这是一种写法

//        System.out.println("access index11111111111111111");

        if(!StringUtils.isEmpty(id)){
            GlobalConfig globalConfig=globalConfigService.selectById(id);
            map.put("globalConfig",globalConfig);
        }
        return new ModelAndView("/globalconfig/modify",map);

    }

    @RequestMapping("/addui")  // 增加ui
    public ModelAndView addui(Map<String,Object> map){
        //map是传给模板的参数，这是一种写法

//        System.out.println("access index11111111111111111");

        return new ModelAndView("/globalconfig/add",map);

    }

    @RequestMapping("/list")
//    @ResponseBody   //注意是否返回模板
    public ModelAndView list(){
//        List<GlobalConfig> list= globalConfigService.selectAll();
//        return ResultVoUtil.success(list);
        List<GlobalConfig> list= globalConfigService.selectAll();
        ModelAndView mav=new ModelAndView("/globalconfig/list");
        mav.addObject("list",list);
        log.info("获取去阿全局配置列表成功{}",list);
        return mav;
    }

    @RequestMapping("/add")
    @ResponseBody
    public ResultVO add(@RequestParam(value = "ckey") String ckey,//对应form的name
                        @RequestParam(value = "cvalue") String cvalue,
                        @RequestParam(value = "status") String status,
                        @RequestParam(value = "description") String description){  //参数为pojo  key-value都要用引号postman在body写 raw-json

        GlobalConfig globalConfig=new GlobalConfig();
        globalConfig.setCkey(ckey);
//        globalConfig.setId(id);   //自增不需要
        globalConfig.setCvalue(cvalue);
        globalConfig.setDescription(description);
        globalConfig.setStatus(status);
        int res= globalConfigService.insertOne(globalConfig);
        if(res>0){
            return ResultVoUtil.success();
        }
        return ResultVoUtil.error(1,"插入失败");
    }

    //https://blog.csdn.net/suki_rong/article/details/80445880
//    @RequestMapping("/add")
//    @ResponseBody
//    public ResultVO add(@RequestBody GlobalConfig globalConfig){  //参数为pojo  key-value都要用引号postman在body写 raw-json
//
//        log.info("access add");
//        System.out.println(globalConfig.toString());
//        int res= globalConfigService.insertOne(globalConfig);
//        if(res>0){
//            return ResultVoUtil.success();
//        }
//        return ResultVoUtil.error(1,"插入失败");
//    }

    // BindingResult bindingResult注意这个参数，如果在参数列表中添加这个参数会报错？？用处？
    @RequestMapping("/update")
    @ResponseBody
    public ResultVO update(@RequestParam(value = "id") int id, //对应form的name
                           @RequestParam(value = "ckey") String ckey,
                           @RequestParam(value = "cvalue") String cvalue,
                           @RequestParam(value = "status") String status,
                           @RequestParam(value = "description") String description
                          ){


//        System.out.println("form:"+form.getCkey());
        GlobalConfig globalConfig=new GlobalConfig();
        globalConfig.setCkey(ckey);
        globalConfig.setId(id);
        globalConfig.setCvalue(cvalue);
        globalConfig.setDescription(description);
        globalConfig.setStatus(status);
        int res=globalConfigService.updateOne(globalConfig);
        if(res>0){
            log.info("修改信源配置成功：{}",globalConfig);
            return ResultVoUtil.success();
        }

        return ResultVoUtil.error(1,"更新失败");

    }

    /**
     * ok  postman:body-raw-son  键值对json字符串  可正常测试
     * @param configid
     * @return
     */
//    @RequestMapping("/update")
//    @ResponseBody
//    public ResultVO update(@RequestBody GlobalConfig globalConfig){
//
//        int res=globalConfigService.updateOne(globalConfig);
//        if(res>0){
//            return ResultVoUtil.success();
//        }
//        return ResultVoUtil.error(1,"更新失败");
//
//    }

    @RequestMapping("/del")
    @ResponseBody
    public ResultVO del(@RequestParam(value = "id") int configid){

        int res=globalConfigService.delById(configid);
        if(res>0){
            return ResultVoUtil.success();
        }
        return ResultVoUtil.error(1,"删除失败");

    }


}
